package cn.com.chengmandian.bike.controller;

import cn.com.chengmandian.bike.model.EbDepositRecord;
import cn.com.chengmandian.bike.service.IEbDepositRecordService;
import cn.com.chengmandian.bike.service.IEbMemberWalletService;
import cn.com.chengmandian.bike.service.IEbPreOrderService;
import cn.com.chengmandian.core.common.constants.CommonConstant;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.service.bike.constants.MemberMoneyBusinessTypeEnum;
import cn.com.chengmandian.service.bike.constants.MemberMoneyOperationTypeEnum;
import cn.com.chengmandian.service.bike.form.*;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Slf4j
@Api(tags = "骑行相关接口接口 - 微服务")
@RestController
@RequestMapping("/inner")
public class InnerMemberController {
    @Autowired
    private IEbMemberWalletService memberWalletService;

    @Autowired
    private IEbDepositRecordService depositRecordService;

    @ApiOperation("用户钱包充值")
    @PostMapping("/wallet/balance")
    public AjaxResult walletBalance(@RequestBody WalletBalanceForm walletBalanceForm) {
        Assert.notNull(walletBalanceForm.getAreaId(), "缺少归属运营区");
        Assert.notNull(walletBalanceForm.getMemberId(), "缺少用户ID");
        Assert.notNull(walletBalanceForm.getMoney(), "缺少金额");
        Assert.notNull(walletBalanceForm.getBusinessType(), "缺少业务类型");
        Assert.notNull(walletBalanceForm.getOperationType(), "缺少操作类型");
        memberWalletService.updateBalance(walletBalanceForm.getAreaId(), walletBalanceForm.getMemberId(),
                walletBalanceForm.getMoney(),
                MemberMoneyBusinessTypeEnum.getByCode(walletBalanceForm.getBusinessType()),
                walletBalanceForm.getBusinessId(),
                MemberMoneyOperationTypeEnum.getByCode(walletBalanceForm.getOperationType()));
        return AjaxResult.success();
    }

    @ApiOperation("用户钱包充值余额退款")
    @PostMapping("/wallet/balance/refund")
    public AjaxResult walletBalanceRefund(@RequestBody WalletBalanceRefundForm form){
        Assert.notNull(form.getMemberId(),"缺少用户id");
        Assert.isTrue(ObjectUtil.isNotNull(form.getRefundMoney()) && form.getRefundMoney().compareTo(BigDecimal.ZERO) > 0,"请输入退款金额");
        memberWalletService.walletBalanceRefund(form);
        return AjaxResult.success();
    }

    @ApiOperation("押金退款")
    @PostMapping("/member/deposit/refund")
    public AjaxResult memberDepositRefund(@RequestBody MemberDepositRefundForm form){
        Assert.notNull(form.getId(), "缺少数据ID");
        Assert.isTrue(StrUtil.isNotBlank(form.getRefundRemark()), "请填写退款原因");

        EbDepositRecord depositRecord = depositRecordService.getById(form.getId());
        Assert.notNull(depositRecord, "未获取到押金记录");
        Assert.notNull(depositRecord.getStatus().equals(CommonConstant.ONE), "押金状态异常，暂不支持退款");

        depositRecordService.refund(depositRecord, form.getRefundRemark(), LocalMapUtil.getAgentInfo(), form.getLoginUser());
        return AjaxResult.success();
    }

    @ApiOperation("完结支付分订单（废弃）")
    @PostMapping("/member/pay/ride/order/pay")
    public AjaxResult rideOrderPay(@RequestBody BikeNoOrderForm form){
        return AjaxResult.success();
    }

    @ApiOperation("修改支付分订单金额（废弃）")
    @PostMapping("/member/modify/serviceorder")
    public AjaxResult modifyServiceOrder(@RequestBody BikeModifyAmountForm form){
        return AjaxResult.success();
    }
}
